//
//  LeaderBillManagerVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/31.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "LeaderBillManagerVC.h"
#import "LeaderAccountingVC.h"
@interface LeaderBillManagerVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation LeaderBillManagerVC
{
    UITableView *_tableView;
    
    NSDictionary *_resActiListDic;
    //请求下来的字典
    NSDictionary *_responseDic;
    //请求下来的数组
    NSArray *_responseArr;
    NSArray *_resArr;
}
- (void)viewWillAppear:(BOOL)animated{
    [self requsetForActityList];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"账单管理";
    self.view.backgroundColor = superBackgroundColor;

//    [self initTopView];
    [self initTableView];
//    [self initBottomView];
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}
- (void)initTopView{
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, 35)];
    topView.backgroundColor = superBackgroundColor;
    [self.view addSubview:topView];
    UILabel *totalLb = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 150, 25)];
    totalLb.text = @"活动预算：10000.00元";
    totalLb.textColor = Theme_Color;
    totalLb.font = [UIFont systemFontOfSize:14];
    totalLb.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:totalLb];
    UILabel *nowSpendLb = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth - 160, 5, 150, 25)];
    NSNumber *_totalMoney = _responseDic[@"totalMoney"];
    nowSpendLb.text = [NSString stringWithFormat:@"%@%@%@",@"目前支出：", _totalMoney,@"元"];
    nowSpendLb.textAlignment = NSTextAlignmentRight;
    nowSpendLb.textColor = Orange_Color;
    nowSpendLb.font = [UIFont systemFontOfSize:13];
    [topView addSubview:nowSpendLb];
}
- (void)initBottomView{
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight-48, KScreenWidth, 48)];
    bottomView.backgroundColor = White_Color;
    UIButton *addBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth/2 - 50, 0, 100, 40)];
    [addBtn setTitle:@"＋ 记一笔" forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [addBtn setTitleColor:Theme_Color forState:UIControlStateNormal];
    [bottomView addSubview:addBtn];
    [self.view addSubview:bottomView];
}
//请求领队活动列表，把正在进行的活动的ID保存下来
- (void)requsetForActityList{
    //参数
    NSDictionary *parameters = @{
                                 @"clubId":[NSString readToNSDefaultsWithKey:kClubId],
                                 @"userId": [NSString readToNSDefaultsWithKey:kId],
                                 @"status": @"2",
                                 @"pageNo": @"1",
                                 @"pageSize": @"20",
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/activity/list.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        _resActiListDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_resActiListDic);
        NSString *error = _resActiListDic[@"code"];
        if (![_resActiListDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }
        if ([_resActiListDic[@"code"] isEqualToString:@"3003"]) {
            [SVProgressHUD showErrorWithStatus:@"暂无正在进行的活动"];
        }else{
            
            [self initBottomView];
            [self requestForBillList];
    }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求活动列表失败"];
    }];
}
//
- (void)requestForBillList{
    //参数
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
    [parameters setValue:[NSString readToNSDefaultsWithKey:kId] forKey:@"userId"];
    [parameters setValue:_resActiListDic[@"list"][0][@"id"] forKey:@"activityID"];
    
    [SVProgressHUD showWithStatus:@"请求中"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/activity/billList.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@><><><><>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]&&![_responseDic[@"code"] isEqualToString:@"3003"]) {
            [SVProgressHUD dismiss];
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }
        if ([_responseDic[@"code"] isEqualToString:@"3003"]) {
            [SVProgressHUD dismiss];
            [SVProgressHUD showErrorWithStatus:@"暂无账单数据"];
        }
        else{
            //请求成功
            [SVProgressHUD dismiss];
//            [SVProgressHUD showSuccessWithStatus:@"账单列表请求成功"];
            [_tableView reloadData];
            [self initTopView];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
    }];
}
- (void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 35, KScreenWidth - 20, KScreenHeight - 50-35)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = superBackgroundColor;
    [self.view addSubview:_tableView];
}
#pragma - mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _responseArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    _resArr = _responseArr[section][@"billList"];
    return _resArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_responseArr.count == 0) {
        return cell;
    }else{
        UILabel *nameLb = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 100, 35)];
        nameLb.text = _responseArr[indexPath.section][@"billList"][indexPath.row][@"payContent"];
        nameLb.font = [UIFont systemFontOfSize:15];
        [cell.contentView addSubview:nameLb];
        
        UILabel *payUserLb = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth/2-60, 0, 100, 35)];
        payUserLb.text = _responseArr[indexPath.section][@"billList"][indexPath.row][@"payerName"];
        payUserLb.textAlignment = NSTextAlignmentCenter;
        payUserLb.font = [UIFont systemFontOfSize:15];
        [cell.contentView addSubview:payUserLb];
        
        UILabel *spendLb = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth - 140, 0, 100, 35)];
        NSNumber *money = _responseArr[indexPath.section][@"billList"][indexPath.row][@"money"];
        NSString *moneyStr = money.stringValue;
        spendLb.text = [@"-" stringByAppendingString:moneyStr];
        spendLb.textAlignment = NSTextAlignmentRight;
        spendLb.font = [UIFont systemFontOfSize:15];
        [cell.contentView addSubview:spendLb];
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, KScreenWidth, 30)];
    if (_responseArr.count == 0) {
        return customView;
    }else{
        customView.backgroundColor = superBackgroundColor;
        UILabel *timeLb = [[UILabel alloc]initWithFrame:CGRectMake(8, 0, 100, 30)];
        timeLb.text = _responseDic[@"list"][section][@"payTime"];
        timeLb.textColor = Gray_Color;
        timeLb.font = [UIFont systemFontOfSize:13];
        [customView addSubview:timeLb];
        return customView;
    }
}
//
- (void)addBtnAction{
    LeaderAccountingVC *vc = [[LeaderAccountingVC alloc]init];
    vc.activityId = _resActiListDic[@"list"][0][@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
